package com.hualan.sfzy.controller.consult;

import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.model.entity.Consult;
import com.hualan.sfzy.model.vo.ConsultVO;
import com.hualan.sfzy.service.impl.consult.ConsultServiceImpl;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/consult")
// 咨询师信息控制类

public class ConsultController {
    @Autowired
    private ConsultServiceImpl service;


    @GetMapping
    // 查找全部咨询师信息
    public Result find() {
        List<Consult> list = service.find();
        return Result.success(list);
    }

    @GetMapping("/{id}")
    // 根据 id 查询咨询师信息
    public Result findById(@PathVariable("id") Integer id) {
        Consult entity = service.findById(id);
        return Result.success(entity);
    }

    @GetMapping("/page")
    // 分页查询咨询师信息
    public Result findPage(@RequestParam(value = CommonConstant.PAGE_NUM, required = false, defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
                           @RequestParam(value = CommonConstant.PAGE_SIZE, required = false, defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize, Consult entity) {

        return Result.success(service.findPage(pageNum, pageSize, entity));
    }

    @PostMapping
    // 添加咨询师信息
    public Result add(@RequestBody Consult entity) {
        return service.add(entity) > 0 ? Result.success() : Result.error();
    }

    @PutMapping
    // 修改咨询师信息
    public Result edit(@RequestBody Consult entity) {
        return service.edit(entity) > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("/{id}")
    // 根据 id 删除咨询师信息
    public Result remove(@PathVariable("id") Integer id) {
        return service.remove(id) > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping
    // 批量删除咨询师信息
    public Result removeBatch(Integer[] ids) {
        return service.removeBatch(ids) > 0 ? Result.success() : Result.error();
    }

    // 小程序 获取咨询师信息
    @PostMapping("/wx")
    public Result wxFind(@RequestParam(value = CommonConstant.PAGE_NUM, required = false, defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
                         @RequestParam(value = CommonConstant.PAGE_SIZE, required = false, defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
                         @RequestBody ConsultVO vo) {
        System.out.println(vo);
        // 获取 expertList
        List<Integer> expertList = vo.getExpertList();

        // 检查 expertList 是否仅包含一个元素且该元素为 0
        if (expertList != null && expertList.size() == 1 && expertList.get(0) == 0) {
            // 如果条件满足，将 expertList 设置为空列表
            vo.setExpertList(null);
        }

        Map<String, Object> map = service.selectBySearchPage(pageNum, pageSize, vo);
        return Result.success(map);
    }


}
